﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AutoTestTools
{
    public partial class DocumentTestCaseForm : Form
    {
        DataTable dtFieldList = new DataTable();

        public DocumentTestCaseForm()
        {
            InitializeComponent();
        }

        private void DocumentTestCaseForm_Load(object sender, EventArgs e)
        {
            chbFieldType.SelectedIndex = 0;
            dtFieldList.Columns.Add("字段信息");
            gvFieldList.DataSource = dtFieldList;
            gvFieldList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string newRow = string.Empty;
            newRow = string.Format(" :{0} => {{:填写内容 =>\"{1}\",:输入类型 => \"{2}\",}}",
                txtFieldName.Text, txtFieldContent.Text, chbFieldType.SelectedItem.ToString());
            dtFieldList.Rows.Add(newRow);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow item in gvFieldList.SelectedRows)
            {
                gvFieldList.Rows.Remove(item);
            }
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                string result = string.Empty;
                StringBuilder sbFileList = new StringBuilder();
                foreach (DataGridViewRow item in gvFieldList.Rows)
                {
                    if (item.Cells[0].Value != null && !string.IsNullOrEmpty(item.Cells[0].Value.ToString()))
                    {
                        sbFileList.AppendFormat("{0},\r\n\t", item.Cells[0].Value);
                    }
                }
                result = string.Format("@@{0}hash = {{ \r\n\t:分类名称 => {{:填写内容 =>\"{1}\",}},\r\n\t{2} :关闭编辑方式 =>\"保存\",\r\n\t}}",
                                        txtModuleName.Text, txtModuleCategory.Text, sbFileList.ToString());
                string fileName = string.Format("{0}/{1}.txt", txtPath.Text, txtModuleName.Text);
                System.IO.File.WriteAllText(fileName, result);
                MessageBox.Show("生成成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                throw;
            }
        }

    }
}
